package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.User;
import com.itheima.utils.BeanUtil;
import com.itheima.web.BaseServlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
    /**
     * 分页查询的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        PageInfo<User> list = userService.findAll(page, size);
        //将查询到的数据请求转发到请求域中
        req.setAttribute("page", list);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(req, resp);
    }

    /**
     * 跳转到添加界面的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //加载所有部门放在请求域中
        List<Dept> list = deptService.findAll();
        req.setAttribute("deptList", list);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(req, resp);
    }

    /**
     * 添加用户的方法
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");
        userService.save(user);
        //添加完成之后,重定向到分页查询
        resp.sendRedirect(req.getContextPath() + "/system/user?operation=list");
    }

    /**
     * 跳转编辑页面的方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        //首先需要数据回显
        User user = userService.findById(id);
        //将数据放在求请域中
        req.setAttribute("user",user);
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/update.jsp").forward(req,resp);
    }

    /**
     * 修改用户信息的方法
     * @param req
     * @param resp
     * @throws IOException
     */
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");
        userService.update(user);
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        userService.delete(id);
        //删除成功后从定向到list界面
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }
}
